package com.atstudying.mall.order.interceptor;

import com.atstudying.common.constant.AuthServerConstant;
import com.atstudying.common.vo.MemberRespVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Mr.zhneg
 * @create 2020-11-21-12:12
 */
@Controller
public class OrderInterceptor implements HandlerInterceptor{

    //有了它就可以共享数据
    public static ThreadLocal<MemberRespVo> loginUser=new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        MemberRespVo attribute = (MemberRespVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute!=null){

            loginUser.set(attribute);
            return true;
        }else {

            //没登录就去登录
            request.getSession().setAttribute("msg","请先进行登录");
            response.sendRedirect("http://auth.mall.com/login.html");
            return false;

        }
    }
}
